home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6440 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  76 lines

  1. Path: sci.kun.nl!usenet
  2. From: Marian Hellema <marian@atcmp.nl>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Virtual Function in private part?
  5. Date: 8 Feb 1996 11:47:11 GMT
  6. Organization: AT Computing, Nijmegen, the Netherlands
  7. Message-ID: <4fcnrv$6q6@wn1.sci.kun.nl>
  8. References: <4fa52o$pkb@news1.usa.pipeline.com> <4fbat4$o80@qualcomm.com>
  9. NNTP-Posting-Host: atcmpg.atcmp.kun.nl
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (X11; I; SunOS 5.4 sun4m)
  14. X-URL: news:4fbat4$o80@qualcomm.com
  15.  
  16. nabbasi@qualcomm.com (Nasser Abbasi) wrote:
  17. >In article <4fa52o$pkb@news1.usa.pipeline.com>, grantp@usa.pipeline.co 
  18. >says...
  19. >> 
  20. >>class A { public:   
  21. >>void Expand { Left = MakeChild(); Right = MakeChild(); } 
  22. >>private: 
  23. >>virtual A * MakeChild() { return new A; } 
  24. >>A * Left;  A * Right;  
  25. >>}; 
  26. >> 
  27. >>class B { 
  28. >> private: 
  29. >>  virtual A* MakeChild() { return new B; } 
  30. >>}; 
  31. >> 
  32. >>Now, when you call Expand, the appropriate types of 
  33. >>objects will be constructed.   
  34. >> 
  35. >> 
  36. >
  37. >In your class B, the  MakeChild() is declared to return pointer to A, 
  38. >yet it returns a pointer to B. Do you not need to do type conversion 
  39. >here?
  40. >
  41. >Will this compile without errors or at least a warning?
  42. >
  43.  
  44. I suppose that class B is meant to be publicly derived from class A.
  45. In that case, you don't need an explicit conversion, because a
  46. baseclass-pointer can point to a derived-class object.
  47.  
  48. By the way, the proposal for the ISO/ANSI standard allows B::MakeChild()
  49. to return a pointer to B, like this:
  50.  
  51. class A {
  52. public:
  53.     void Expand() { Left = MakeChild(); Right = MakeChild(); }
  54. private:
  55.     virtual A * MakeChild() { return new A; }
  56.     A * Left;  A * Right;
  57. };
  58.  
  59. class B : public A {
  60. private:
  61.     virtual B* MakeChild() { return new B; }
  62. };
  63.  
  64.  
  65. The restriction is that the returntype in the derived class is
  66. a pointer or reference to a class that is publicly derived from
  67. the returntype in the baseclass.
  68.  
  69. Marian
  70. -- 
  71. ----------------------------------------------------------------------
  72. Marian Hellema            AT Computing, UNIX training and consultancy
  73. email: marian@atcmp.nl    P.O. Box 1428, 6501 BK Nijmegen
  74. phone: +31 24 3527225     the Netherlands
  75.  
  76.